//
//  VPaiUpgradeManager.swift
//  VPai
//
//  Created by devel on 16/10/18.
//  Copyright © 2016年 viatech. All rights reserved.
//

import Foundation
//import Alamofire
import SwiftyJSON
import Async

/*
 一个管理相机升级信息的类
 相机不直接连接服务器检查和下载更新，而是先通过手机客户端去检查和下载，再将升级包
 传给相机升级。
 */
class VPaiUpgradeManager{
    
    static let ERR_UPGRADE_GET_DEVICEINFO = -1 //获取相机信息时出错，升级前要确定相机已插入sdcard和在充电状态
    static let ERR_UPGRADE_NO_CHARGE = -2 //不在充电状态
    static let ERR_UPGRADE_NO_SDCARD = -3 //没有sdcard
    static let ERR_UPGRADE_REQUEST_UPDATE = -4 //向相机发送升级请求出错
    static let ERR_UPGRADE_FIREWARE_FILE = -5 //升级文件出错
    static let ERR_UPGRADE_WEBSOCKET_CLOSE = -6 //与相机断开连接
    static let ERR_UPGRADE_SEND_FIREWARE = -7 //发送升级包错误

    
    init() {
       // vpaiStorage = VPaiStorage()
      //  checkAllDeviceUpgrede()
        
        vpaiCtrlWebSocketEvent = VPaiCtrlWebSocket.shareInstance.registerEvent()
        vpaiCtrlWebSocketEvent?.onWebSocketClose = {
//            if self.upgrading {
//                self.upgrading = false
//                self.delegate?.onVPaiUpgradeError(VPaiUpgradeManager.ERR_UPGRADE_WEBSOCKET_CLOSE,
//                                                  error: "websocket close")
//            }
        }
        vpaiCtrlWebSocketEvent?.onUpdatedeviceResponse = {percent in
                   }
        
     //   delegate = self
    }
    
    deinit {
        if let event = vpaiCtrlWebSocketEvent {
            VPaiCtrlWebSocket.shareInstance.unregisterEvent(event)
        }
    }

    func getUpgradeInfo(info: VPaiDeviceInfo) -> UpgradeInfo? {
              return nil
    }
    
    func startUpgrade(info: UpgradeInfo){
//        Alert.showCancelConfirmAlert("dialog message for upgrade".i18n+"\(info.chagelog)", title: nil, cancleCB: nil){
//            Alert.showCancelConfirmAlert("dialog message for upgrade tip".i18n, title: nil, cancleCB: nil) {
//                self.doStartUpgrade(info)
//            }
//        }
    }
    
    //开始升级流程
    func onVPaiUpgradeStart(){
      
    }
    //升级过程中发生错误，升级中断
    func onVPaiUpgradeError(ret: Int, error: String){
      //  Log.info(VPaiUpgradeManager.TAG, msg: "onVPaiUpgradeError:ret = \(ret), error = \(error)")

        let string : String
        switch ret {
        case VPaiUpgradeManager.ERR_UPGRADE_GET_DEVICEINFO:
            string = "Get camera info error".i18n
        case VPaiUpgradeManager.ERR_UPGRADE_NO_CHARGE:
            string = "Camera is not charging".i18n
        case VPaiUpgradeManager.ERR_UPGRADE_NO_SDCARD:
            string = "Camera has not sdcard".i18n
        case VPaiUpgradeManager.ERR_UPGRADE_REQUEST_UPDATE:
            string = "Send request error".i18n
        case VPaiUpgradeManager.ERR_UPGRADE_REQUEST_UPDATE:
            string = "Fireware file error".i18n
        case VPaiUpgradeManager.ERR_UPGRADE_REQUEST_UPDATE:
            string = "Lose connection with camera".i18n
        case VPaiUpgradeManager.ERR_UPGRADE_REQUEST_UPDATE:
            string = "Send fireware error".i18n
        default:
            string = "Unknow error".i18n
        }
       // ProgressHUD.shareInstance.showErrorWithString(string)
    }


    
  //  private static let TAG = "VPaiUpgradeManager"
    //private let vpaiStorage : VPaiStorage
    private var upgrading = false
    private var vpaiCtrlWebSocketEvent : VPaiCtrlWebSocketEvents?
    

    


    
       //ui线程
    private func sendUpdateRequest2Device(info: UpgradeInfo) {
        let fileManager = NSFileManager.defaultManager()
        do {
           
                   }catch {
          //  Log.error(VPaiUpgradeManager.TAG, msg: "startUpgrade: \(error)")
        }
    }
    
}

class UpgradeInfo {
    var url = ""
    var chagelog = ""
    var fromversion = 0
    var toversion = 0
    var model = ""
    var path = ""
    var product = ""
    var manufactory = ""
    
    func toString() -> String {
        return "[url=\(url),changelog=\(chagelog),fromversion=\(fromversion),toversion=\(toversion), model=\(model),path=\(path),product=\(product),manufactory=\(manufactory)]"
    }
}
